package com.wenge.datagroup.storage.util;

public class IdWorker {
//	private final long workerId;
	private final static long TWEPOCH = 1288834974657L;
	private long sequence = 0L;
	/* work_id指定位数 */
	private final static long WORKER_ID_BITS = 8L;
	/* �?大workid */
	public final static long MAX_WORKER_ID = -1L ^ -1L << WORKER_ID_BITS;
	/* 序列号位�? */
	private final static long SEQUENCE_BITS = 6L;
	/* workid 位移 */
	private final static long WORKER_ID_SHIFT = SEQUENCE_BITS;
	/* 时间戳左移位�? */
	private final static long TIMESTAMP_LEFT_SHIFT = SEQUENCE_BITS + WORKER_ID_BITS;
	/* 序列号掩�? */
	public final static long SEQUENCE_MASK = -1L ^ -1L << SEQUENCE_BITS;
	private long lastTimestamp = -1L;
	
	private static final class IdWorkerHoder{
		public static final IdWorker INSTANCE = new IdWorker();
	}
	
	public static final IdWorker getInstance(){
		return IdWorkerHoder.INSTANCE;
	}

	private IdWorker() {
		super();
	}

	public synchronized long nextId(long workerId) {
		long timestamp = this.timeGen();
		if (this.lastTimestamp == timestamp) {
			this.sequence = (this.sequence + 1) & SEQUENCE_MASK;
			if (this.sequence == 0) {
				System.out.println("###########" + SEQUENCE_MASK);
				timestamp = this.tilNextMillis(lastTimestamp);
			}
		} else {
			this.sequence = 0;
		}
		if (timestamp < this.lastTimestamp) {
			try {
				throw new Exception(
						String.format(
								"Clock moved backwards. Refusing to generate id for %d milliseconds",
								this.lastTimestamp - timestamp));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		lastTimestamp = timestamp;
		long nextId = ((timestamp - TWEPOCH << TIMESTAMP_LEFT_SHIFT))
				| (workerId << WORKER_ID_SHIFT) | (sequence);
		return nextId;
	}
	
	public synchronized long nextId(Integer workerId) {
		long timestamp = this.timeGen();
		if (this.lastTimestamp == timestamp) {
			this.sequence = (this.sequence + 1) & SEQUENCE_MASK;
			if (this.sequence == 0) {
				System.out.println("###########" + SEQUENCE_MASK);
				timestamp = this.tilNextMillis(lastTimestamp);
			}
		} else {
			this.sequence = 0;
		}
		if (timestamp < this.lastTimestamp) {
			try {
				throw new Exception(
						String.format(
								"Clock moved backwards. Refusing to generate id for %d milliseconds",
								this.lastTimestamp - timestamp));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		lastTimestamp = timestamp;
		long nextId = ((timestamp - TWEPOCH << TIMESTAMP_LEFT_SHIFT))
				| (workerId << WORKER_ID_SHIFT) | (sequence);
		return nextId;
	}

	private long tilNextMillis(final long lastTimestamp) {
		long timestamp = this.timeGen();
		while (timestamp <= lastTimestamp) {
			timestamp = this.timeGen();
		}
		return timestamp;
	}

	private long timeGen() {
		return System.currentTimeMillis();
	}

	public static void main(String[] args) {
		long id = IdWorker.getInstance().nextId(20);
		System.err.println(" --->>>> "+id);

		for (int i = 0; i < 10; i++) {
			new Thread(new Runnable() {
				@Override
				public void run() {
					for(int i=0;i<10;i++){
						long id2 = IdWorker.getInstance().nextId(20);
					}
				}
			}).start();

		}
		for(int i=0;i<100;i++){
			id = IdWorker.getInstance().nextIdTest(20);
		}
	}

	public synchronized long nextIdTest(long workerId) {
		long timestamp = this.timeGen();
		if (this.lastTimestamp == timestamp) {
			this.sequence = (this.sequence + 1) & SEQUENCE_MASK;
			if (this.sequence == 0) {
				System.out.println("###########" + SEQUENCE_MASK);
				timestamp = this.tilNextMillis(lastTimestamp);
			}
		} else {
			this.sequence = 0;
		}
		if (timestamp < this.lastTimestamp) {
			try {
				throw new Exception(
						String.format(
								"Clock moved backwards. Refusing to generate id for %d milliseconds",
								this.lastTimestamp - timestamp));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		lastTimestamp = timestamp;
		long nextId = ((timestamp - TWEPOCH << TIMESTAMP_LEFT_SHIFT))
				| (workerId << WORKER_ID_SHIFT) | (sequence);
		System.out.println("timestamp:" + timestamp + ",timestampLeftShift:"
				+ TIMESTAMP_LEFT_SHIFT + ",nextId:" + nextId + ",workerId:"
				+ workerId + ",sequence:" + sequence);
		return nextId;
	}
}